package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.SysBankAccountConfigBo;
import com.bigdata.service.domain.vo.SysBankAccountConfigVo;
import com.bigdata.service.domain.SysBankAccountConfig;
import com.bigdata.service.mapper.SysBankAccountConfigMapper;
import com.bigdata.service.service.ISysBankAccountConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 平台系统配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-20
 */
@RequiredArgsConstructor
@Service
public class SysBankAccountConfigServiceImpl implements ISysBankAccountConfigService {

    private final SysBankAccountConfigMapper baseMapper;

    /**
     * 查询平台系统配置
     */
    @Override
    public SysBankAccountConfigVo queryById(Long configId){
        return baseMapper.selectVoById(configId);
    }

    /**
     * 查询平台系统配置列表
     */
    @Override
    public TableDataInfo<SysBankAccountConfigVo> queryPageList(SysBankAccountConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysBankAccountConfig> lqw = buildQueryWrapper(bo);
        Page<SysBankAccountConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询平台系统配置列表
     */
    @Override
    public List<SysBankAccountConfigVo> queryList(SysBankAccountConfigBo bo) {
        LambdaQueryWrapper<SysBankAccountConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBankAccountConfig> buildQueryWrapper(SysBankAccountConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBankAccountConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPayeeName()), SysBankAccountConfig::getPayeeName, bo.getPayeeName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountNumber()), SysBankAccountConfig::getAccountNumber, bo.getAccountNumber());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), SysBankAccountConfig::getBankName, bo.getBankName());
        lqw.like(StringUtils.isNotBlank(bo.getBranchName()), SysBankAccountConfig::getBranchName, bo.getBranchName());
        lqw.eq(bo.getIsActive() != null, SysBankAccountConfig::getIsActive, bo.getIsActive());
        lqw.eq(StringUtils.isNotBlank(bo.getPayImageUrl()), SysBankAccountConfig::getPayImageUrl, bo.getPayImageUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatformIntroduction()), SysBankAccountConfig::getPlatformIntroduction, bo.getPlatformIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getPrivacyAndCopyright()), SysBankAccountConfig::getPrivacyAndCopyright, bo.getPrivacyAndCopyright());
        lqw.orderByDesc(SysBankAccountConfig::getCreateTime);
        return lqw;
    }

    /**
     * 新增平台系统配置
     */
    @Override
    public Boolean insertByBo(SysBankAccountConfigBo bo) {
        SysBankAccountConfig add = BeanUtil.toBean(bo, SysBankAccountConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setConfigId(add.getConfigId());
        }
        return flag;
    }

    /**
     * 修改平台系统配置
     */
    @Override
    public Boolean updateByBo(SysBankAccountConfigBo bo) {
        SysBankAccountConfig update = BeanUtil.toBean(bo, SysBankAccountConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysBankAccountConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除平台系统配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
